<?php

namespace Admin\Model\DBO;

use Zend\Db\TableGateway\AbstractTableGateway;
use Admin\Entity\User;

class UserDBO extends AbstractTableGateway {

    private $sm;
    private $userTable;

    public function __construct($serviceManager) {
        $this->sm = $serviceManager;
    }

    public function getUserTable() {
        if (!$this->userTable) {
            $this->userTable = $this->sm->get('Admin\Model\UserTable');
        }
        return $this->userTable;
    }

    /*
     * Save user profile
     * @ return boolean
     */

    public function fetchAll($paginator = false) {
        return $this->getUserTable()->fetchAll($paginator);
    }

    public function saveUser(User $user) {
        // kiem tra xem userName da ton tai hay chua, neu chua ton tai thi luu
        if ($this->getUserID($user->userName)) {
            return false;
        } else {
            // Neu chua ton tai User nay thi luu vao bang User
            $this->getUserTable()->saveUser($user);
            return true;
        }
    }

    public function getUserID($userName) {
        $user = $this->getUserTable()->getUserID($userName);
        if (!$user)
            return null;
        else
            return $user;
    }

    public function getUser($userID) {
        return $this->getUserTable()->getUser($userID);
    }

    public function updateUserInfo(User $user, $userID) {
        $data = array(
            'roleID' => $user->roleID,
            'userName' => $user->userName,
            'password' => $user->password,
            'fullName' => $user->fullName,
            'lastName' => $user->lastName,
            'sex' => $user->sex,
            'birthday' => $user->birthday,
            'email' => $user->email,
            'phoneNumber' => $user->phoneNumber,
            'residentAddress' => $user->residentAddress,
            'nativePlace' => $user->nativePlace,
            'profilePhoto' => $user->profilePhoto,
            'description' => $user->description,
        );
        $this->getUserTable()->updateInfoUser($data, $userID);
    }
    public function deleteUser($userID) {
        $this->getUserTable()->deleteUser($userID);
    }
}

?>